import { defineStore } from "pinia";
import { userLogin } from "../api/user";
import { User, UserInfoType } from "../types/user";
import router from "../router";
import { useCart } from "./cart";
interface UserStateType {
  userInfo: UserInfoType | null;
}
export const useUserStore = defineStore("user", {
  state: (): UserStateType => ({
    userInfo: null,
  }),
  actions: {
    async userLoginAction(user: User) {
      let res = await userLogin(user);
      this.userInfo = res.data;
      console.log(router.currentRoute.value.query.target);
      // 初始化用户购物车数据
      const cart = useCart();
      await cart.initCartAction(res.data.objectId);
      let { target } = router.currentRoute.value.query;
      let path = target ? target : "/home";
      router.push(path as string);
    },
  },
  persist: {
    key: "pinia-user-2206",
  },
});
